//
// Created by Win on 2017/1/6.
//

#include <frlog.h>
#include <sys/time.h>
#include "frbyte_buffer.h"
#include "frhessian.h"
#include "frreport_comm.h"

#define ARRAY_LEN           256

union int32_float_bits {
    int32_t int_bits;
    float float_bits;
};

union int64_double_bits {
    int64_t int_bits;
    double double_bits;
};
/*!
 * \brief hessian readCall 调用一个接口
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_read_call(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    int i_ret = 0;
    char arr_key[ARRAY_LEN];
    char arr_value[8];
    uint32_t u32_major = 0, u32_minor = 0;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_YES || p_st_parse == NULL)
        return FR_FAIL;
    
    // \x63  + version 开头
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 99)
    {
        bytebuffer_backspace(p_st_data, 1);
        return FR_FAIL;
    }
    
    //version major
    i_ret = bytebuff_get_int(p_st_data, 1, &u32_major, BYTE_BIG_ENDIAN);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    //version minor
    i_ret = bytebuff_get_int(p_st_data, 1, &u32_minor, BYTE_BIG_ENDIAN);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    
    snprintf(arr_value, sizeof(arr_value), "%d.%d", u32_major, u32_minor);
    snprintf(arr_key, sizeof(arr_key), "%s.version", p_prefix);
    
    i_ret = frreport_body_l7_add_str(p_st_body_data, arr_key, arr_value);
    
    return i_ret;
    
}


/*!
 * \brief hessian 获取一个char UTF8
 *
 * \arg         p_st_data 数据内容
 *              p_st_parse hessian 数据相关信息
 *
 * \return      >=0:成功
 *              -1:失败
 *
 */
FR_STATIC int __frhessian_parse_utf8(Frbyte_Packet *p_st_data, hessian_parse *p_st_parse)
{
    int ch = 0, ch1 = 0, ch2 = 0;
    
    bytebuff_get_int(p_st_data, 1, (uint32_t *)&ch, BYTE_BIG_ENDIAN);
    
    if(ch < 128)
        return ch;
    else
    {
        if((ch & 224) == 192)
        {
            bytebuff_get_int(p_st_data, 1, (uint32_t *)&ch1, BYTE_BIG_ENDIAN);
            ch2 = ((ch & 31) << 6) + (ch1 & 63);
            return ch2;
        }
        else if((ch & 240) == 224)
        {
            bytebuff_get_int(p_st_data, 1, (uint32_t *)&ch1, BYTE_BIG_ENDIAN);
            bytebuff_get_int(p_st_data, 1, (uint32_t *)&ch2, BYTE_BIG_ENDIAN);
            int v = ((ch & 15) << 12) + ((ch1 & 63) << 6) + (ch2 & 63);
            return v;
        }
        else
        {
            bytebuffer_backspace(p_st_data, 1);
            FRLOG_ERROR_DETAIL("bad utf-8 encoding at %d", ch);
            return -1;
        }
    }
    
}

/*!
 * \brief hessian 获取一个char
 *
 * \arg         p_st_data 数据内容
 *              p_st_parse hessian 数据相关信息
 *
 * \return      >=0:成功
 *              -1:失败
 *
 */
FR_STATIC int __frhessian_parse_char(Frbyte_Packet *p_st_data, hessian_parse *p_st_parse)
{
    uint8_t u8_code = 0;
    
    if(p_st_data == NULL || p_st_parse == NULL)
        return -1;
    
    while (p_st_parse->s_chunkLength <= 0)
    {
        if(p_st_parse->i_isLastChunk)
            return -1;
        
        bytebuff_get_char(p_st_data, &u8_code);
        switch (u8_code)
        {
            case 83:
            case 88:
                p_st_parse->i_isLastChunk = FR_YES;
                bytebuff_get_short(p_st_data, 2, (uint16_t *)&p_st_parse->s_chunkLength, BYTE_BIG_ENDIAN);
                break;
    
            case 115:
            case 120:
                p_st_parse->i_isLastChunk = FR_NO;
                bytebuff_get_short(p_st_data, 2, (uint16_t *)&p_st_parse->s_chunkLength, BYTE_BIG_ENDIAN);
                break;
    
            default:
                bytebuffer_backspace(p_st_data, 1);
                FRLOG_ERROR_DETAIL("expect string code[code is %d]", u8_code);
                return -1;
                
        }
        
    }
    
    --p_st_parse->s_chunkLength;
    //return this.parseUTF8Char();
    return __frhessian_parse_utf8(p_st_data, p_st_parse);
    
}

/*!
 * \brief hessian 获取一个byte
 *
 * \arg         p_st_data 数据内容
 *              p_st_parse hessian 数据相关信息
 *
 * \return      >=0:成功
 *              -1:失败
 *
 */
FR_STATIC int __frhessian_parse_byte(Frbyte_Packet *p_st_data, hessian_parse *p_st_parse)
{
    uint8_t u8_code = 0;
    uint8_t ch = 0;
    
    if(p_st_data == NULL || p_st_parse == NULL)
        return -1;
    
    while (p_st_parse->s_chunkLength <= 0)
    {
        if(p_st_parse->i_isLastChunk)
            return -1;
    
        bytebuff_get_char(p_st_data, &u8_code);
        switch (u8_code)
        {
            case 66:
                p_st_parse->i_isLastChunk = FR_YES;
                bytebuff_get_short(p_st_data, 2, (uint16_t *)p_st_parse->s_chunkLength, BYTE_BIG_ENDIAN);
                break;
    
            case 98:
                p_st_parse->i_isLastChunk = FR_NO;
                bytebuff_get_short(p_st_data, 2, (uint16_t *)p_st_parse->s_chunkLength, BYTE_BIG_ENDIAN);
                break;
    
            default:
                bytebuffer_backspace(p_st_data, 1);
                FRLOG_ERROR_DETAIL("except byte[] %d", u8_code);
                return -1;
        }
        
    }
    
    --p_st_parse->s_chunkLength;
    bytebuff_get_char(p_st_data, &ch);
    return ch;
    
}

/*!
 * \brief hessian readHead 注释？
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
char* frhessian_read_head(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    int i_ret = 0;
    uint32_t u32_chunk_len = 0;
    char *p_value = NULL;
    int i_str_begin = 0, i_str_end = 0;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_YES || p_st_parse == NULL)
        return NULL;
    
    // \x48
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 72)
    {
        bytebuffer_backspace(p_st_data, 1);
        return NULL;
    }
    
    p_st_parse->i_isLastChunk = FR_YES;
    i_ret = bytebuff_get_short(p_st_data, 2, (uint16_t *)&p_st_parse->s_chunkLength, BYTE_BIG_ENDIAN);
    if(i_ret == FR_FAIL)
        return NULL;
    
    //todo UTF8 获取
    i_str_begin = bytebuff_getPosition(p_st_data);
    while (__frhessian_parse_char(p_st_data, p_st_parse) >= 0);
    i_str_end = bytebuff_getPosition(p_st_data);
    
    u32_chunk_len = (uint32_t)i_str_end - i_str_begin;
    
    p_value = (char *)frmalloc(u32_chunk_len + 1);
    if(p_value == NULL)
        return NULL;
    
    bytebuffer_backspace(p_st_data, u32_chunk_len);
    bytebuff_get_bytes(p_st_data, p_value, u32_chunk_len);
    
    p_value[u32_chunk_len] = '\0';
    
    return p_value;
    
}

/*!
 * \brief hessian 调用的方法 参数
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_read_method(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    int i_ret = 0;
    uint16_t u16_len = 0;
    char *p_method = NULL;
    char arr_key[ARRAY_LEN];
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_YES || p_st_parse == NULL)
        return FR_FAIL;
    
    // \x6d
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 109)
    {
        bytebuffer_backspace(p_st_data, 1);
        return FR_FAIL;
    }
    
    //长度
    i_ret = bytebuff_get_short(p_st_data, 2, &u16_len, BYTE_BIG_ENDIAN);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    p_method = (char *)frmalloc(u16_len + 1);
    if(p_method == NULL)
        return FR_FAIL;
    
    i_ret = bytebuff_get_bytes(p_st_data, p_method, u16_len);
    if(i_ret == FR_FAIL)
    {
        frfree(p_method);
        return FR_FAIL;
    }
    
    p_method[u16_len] = '\0';
    snprintf(arr_key, sizeof(arr_key), "%s.method", p_prefix);
    
    i_ret = frreport_body_l7_add_str(p_st_body_data, arr_key, p_method);
    if(i_ret == FR_FAIL)
    {
        frfree(p_method);
        return FR_FAIL;
    }
    
    frfree(p_method);
    return FR_SUCC;
    
}

/*!
 * \brief hessian 获取double
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
FR_STATIC int frhessian_get_double(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name)
{
    uint64_t u64_len = 0;
    double db_value = 0.0;
    union int64_double_bits u_64bits;
    int i_ret = 0;
    
    if (p_st_body_data == NULL || p_st_data == NULL)
        return FR_FAIL;
    
    i_ret = bytebuff_get_long(p_st_data, 8, &u64_len, BYTE_BIG_ENDIAN);
    if (i_ret == FR_FAIL)
        return FR_FAIL;
    
    u_64bits.int_bits = u64_len;
    db_value = u_64bits.double_bits;
    
    frreport_body_l7_add_double(p_st_body_data, p_name, db_value);
    return FR_SUCC;
}

/*!
 * \brief hessian 获取 int
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
FR_STATIC int frhessian_get_int(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name)
{
    uint32_t u32_value = 0;
    int i_ret = 0;
    
    if (p_st_body_data == NULL || p_st_data == NULL)
        return FR_FAIL;
    
    i_ret = bytebuff_get_int(p_st_data, 4, &u32_value, BYTE_BIG_ENDIAN);
    if (i_ret == FR_FAIL)
        return FR_FAIL;
    
    frreport_body_l7_add_int(p_st_body_data, p_name, u32_value);
    return FR_SUCC;
}

/*!
 * \brief hessian 获取long
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
FR_STATIC int frhessian_get_long(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name)
{
    uint64_t u64_value = 0;
    int i_ret = 0;
    
    if (p_st_body_data == NULL || p_st_data == NULL)
        return FR_FAIL;
    
    i_ret = bytebuff_get_long(p_st_data, 8, &u64_value, BYTE_BIG_ENDIAN);
    if (i_ret == FR_FAIL)
        return FR_FAIL;
    
    frreport_body_l7_add_long(p_st_body_data, p_name, (long)u64_value);
    return FR_SUCC;
}

/*!
 * \brief hessian 获取 string
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
FR_STATIC int frhessian_get_string(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name)
{
    uint32_t u32_len = 0;
    uint8_t u8_tag = 0;
    char *p_value = NULL;
    Frstr *p_st_str = NULL;
    int i_ret = 0;
    int i_str_start = 0, i_str_end = 0, i_len = 0;
    hessian_parse st_parse = {FR_NO, -1, 0};
    
    if (p_st_body_data == NULL || p_st_data == NULL)
        return FR_FAIL;
    
    p_st_str = frstr_new(1024);
    
    while (st_parse.i_isLastChunk != FR_YES)
    {
        bytebuff_get_char(p_st_data, &u8_tag);
        st_parse.i_isLastChunk = u8_tag == 'S';
    
        i_ret = bytebuff_get_int(p_st_data, 2, &u32_len, BYTE_BIG_ENDIAN);
        if (i_ret == FR_FAIL)
        {
            frstr_free(&p_st_str);
            return FR_FAIL;
        }
        
        st_parse.s_chunkLength = (short)u32_len;
        i_str_start = bytebuff_getPosition(p_st_data);
        while (__frhessian_parse_char(p_st_data, &st_parse) >= 0)
        {
            if(st_parse.s_chunkLength == 0)
            {
                i_str_end = bytebuff_getPosition(p_st_data);
                i_len = i_str_end - i_str_start;
                bytebuffer_backspace(p_st_data, (size_t)i_len);
                
                p_value = (char *)frmalloc((size_t)i_len);
                if(p_value == NULL)
                {
                    frstr_free(&p_st_str);
                    return FR_FAIL;
                }
    
                i_ret = bytebuff_get_bytes(p_st_data, p_value, (size_t)i_len);
                if(i_ret == FR_FAIL)
                {
                    frstr_free(&p_st_str);
                    frfree(p_value);
                    return FR_FAIL;
                }
    
                frstr_append(p_st_str, p_value, (size_t)i_len);
                
                i_str_start = i_str_end + 2;
            }
        }
    
        frstr_append(p_st_str, "\0", 1);
        
        frfree(p_value);
    }
    
    frstr_append(p_st_str, "\0", 1);
    
    p_value = frstr_free2(&p_st_str);
    i_ret = frreport_body_l7_add_str(p_st_body_data, p_name, p_value);
    if(i_ret == FR_FAIL)
    {
        frfree(p_value);
        return FR_FAIL;
    }
    
    frfree(p_value);
    return FR_SUCC;
}

/*!
 * \brief hessian 获取 bytes
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
FR_STATIC int frhessian_get_bytes(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name)
{
    uint32_t u32_len = 0;
    uint8_t u8_tag = 0;
    char *p_value = NULL;
    Frstr *p_st_str = NULL;
    int i_ret = 0;
    int i_isLastChunk = 0;
    
    if (p_st_body_data == NULL || p_st_data == NULL)
        return FR_FAIL;
    
    p_st_str = frstr_new(1024);
    
    while (i_isLastChunk != 1)
    {
        bytebuff_get_char(p_st_data, &u8_tag);
        i_isLastChunk = u8_tag == 'B';
        
        i_ret = bytebuff_get_int(p_st_data, 2, &u32_len, BYTE_BIG_ENDIAN);
        if (i_ret == FR_FAIL)
        {
            frstr_free(&p_st_str);
            return FR_FAIL;
        }
        
        p_value = (char *)frmalloc(u32_len + 1);
        if(p_value == NULL)
        {
            frstr_free(&p_st_str);
            return FR_FAIL;
        }
        
        i_ret = bytebuff_get_bytes(p_st_data, p_value, u32_len);
        if(i_ret == FR_FAIL)
        {
            frstr_free(&p_st_str);
            frfree(p_value);
            return FR_FAIL;
        }
        
        frstr_append(p_st_str, p_value, u32_len);
        frfree(p_value);
    }
    
    u32_len = p_st_str->ui_len;
    p_value = frstr_free2(&p_st_str);
    i_ret = frreport_body_l7_add_bin(p_st_body_data, p_name, p_value, u32_len);
    if(i_ret == FR_FAIL)
    {
        frfree(p_value);
        return FR_FAIL;
    }
    
    frfree(p_value);
    return FR_SUCC;
}

/*!
 * \brief hessian 获取 type
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_st_parse      解析参数
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
char* frhessian_read_type(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, hessian_parse *p_st_parse)
{
    uint8_t u8_code = 0;
    char *p_type = NULL;
    size_t i_chunklen = 0;
    int i_str_begin = 0, i_str_end = 0;
    
    bytebuff_get_char(p_st_data, &u8_code);
    if(u8_code != 116)
    {
        bytebuffer_backspace(p_st_data, 1);
        return NULL;
    }
    else
    {
        p_st_parse->i_isLastChunk = FR_YES;
        bytebuff_get_short(p_st_data, 2, (uint16_t *)&p_st_parse->s_chunkLength, BYTE_BIG_ENDIAN);
        i_chunklen = (size_t)p_st_parse->s_chunkLength;
    
        i_str_begin = bytebuff_getPosition(p_st_data);
        while (__frhessian_parse_char(p_st_data, p_st_parse) >= 0);
        i_str_end = bytebuff_getPosition(p_st_data);
    
        i_chunklen = (uint32_t)i_str_end - i_str_begin;
    
        p_type = (char *)frmalloc(i_chunklen + 1);
        if(p_type == NULL)
            return NULL;
    
        bytebuffer_backspace(p_st_data, i_chunklen);
        bytebuff_get_bytes(p_st_data, p_type, i_chunklen);
    
        p_type[i_chunklen] = '\0';
    
        return p_type;
        
    }
    
    return NULL;
    
}


/*!
 * \brief hessian 获取 date
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
FR_STATIC int frhessian_get_date(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name)
{
    uint64_t u64_value = 0;
    int i_ret = 0;
    
    i_ret = bytebuff_get_long(p_st_data, 8, &u64_value, BYTE_BIG_ENDIAN);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    return frreport_body_l7_add_str(p_st_body_data, p_name, asctime(gmtime((const time_t *)&u64_value)));
    
}

/*!
 * \brief hessian 获取 list
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *              p_st_parse      解析参数
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_get_list(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name, hessian_parse *p_st_parse)
{
    char arr_key[ARRAY_LEN];
    uint32_t i_index = 0;
    uint32_t u32_len = 0;
    uint8_t ch = 0;
    int i_ret = 0;
    
    i_ret = bytebuff_get_char(p_st_data, &ch);
    if(i_ret == FR_FAIL || ch != 'l')
        return FR_FAIL;
    
    bytebuff_get_int(p_st_data, 4, &u32_len, BYTE_BIG_ENDIAN);
    
    //todo
    snprintf(arr_key, sizeof(arr_key), "%s.remoteid", p_name);
    frreport_body_l7_add_int(p_st_body_data, arr_key, p_st_parse->i_ref_id);
    p_st_parse->i_ref_id++;
    
    for (i_index = 1; i_index <= u32_len; i_index++)
    {
        snprintf(arr_key, sizeof(arr_key), "%s.%d", p_name, i_index);
        i_ret = frhessian_read_object(p_st_data, p_st_body_data, arr_key, p_st_parse);
        if(i_ret == FR_FAIL)
            return FR_FAIL;
    }
    
    i_ret = bytebuff_get_char(p_st_data, &ch);
    if(i_ret == FR_FAIL || ch != 'z')
        return FR_FAIL;
    
    snprintf(arr_key, sizeof(arr_key), "%s.size", p_name);
    frreport_body_l7_add_int(p_st_body_data, arr_key, u32_len);
    return FR_SUCC;
    
}

/*!
 * \brief hessian 获取 map
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *              p_st_parse      解析参数
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_get_map(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name, hessian_parse *p_st_parse)
{
    char arr_key[ARRAY_LEN];
    int i_ret = 0;
    uint8_t ch = 0;
    int i_index = 0;
    
    
    //value
    snprintf(arr_key, sizeof(arr_key), "%s.remoteid", p_name);
    i_ret = frreport_body_l7_add_int(p_st_body_data, arr_key, p_st_parse->i_ref_id);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    p_st_parse->i_ref_id++;
    
    //key
    while (ch!='z')
    {
        i_index++;
        
        snprintf(arr_key, sizeof(arr_key), "%s.%d.key", p_name, i_index);
        i_ret = frhessian_read_object(p_st_data, p_st_body_data, arr_key, p_st_parse);
        if(i_ret == FR_FAIL)
            return FR_FAIL;
    
        //value
        snprintf(arr_key, sizeof(arr_key), "%s.%d.value", p_name, i_index);
        i_ret = frhessian_read_object(p_st_data, p_st_body_data, arr_key, p_st_parse);
        if(i_ret == FR_FAIL)
            return FR_FAIL;
        
        i_ret = bytebuff_peek_char(p_st_data, &ch);
        if(i_ret == FR_FAIL)
            return FR_FAIL;
    }
    
    bytebuff_skip(p_st_data, 1);
    
    snprintf(arr_key, sizeof(arr_key), "%s.size", p_name);
    frreport_body_l7_add_int(p_st_body_data, arr_key, i_index);
    
    return FR_SUCC;
}

/*!
 * \brief hessian 获取 type并上报
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *              p_st_parse      解析参数
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_get_type(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name, hessian_parse *p_st_parse)
{
    char *p_value = NULL;
    char arr_key[ARRAY_LEN];
    
    p_value = frhessian_read_type(p_st_data, p_st_body_data, p_st_parse);
    if(p_value == NULL)
        return FR_SUCC;
    
    snprintf(arr_key, sizeof(arr_key), "%s.type", p_name);
    frreport_body_l7_add_str(p_st_body_data, arr_key, p_value);
    frfree(p_value);
    return FR_SUCC;
}

/*!
 * \brief hessian 获取 ref
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *              p_st_parse      解析参数
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_get_ref(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name, hessian_parse *p_st_parse)
{
    char arr_key[ARRAY_LEN];
    uint32_t u32_id = 0;
    int i_ret = 0;
    
    i_ret = bytebuff_get_int(p_st_data, 4, &u32_id, BYTE_BIG_ENDIAN);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    snprintf(arr_key, sizeof(arr_key), "%s.ref.id", p_name);
    frreport_body_l7_add_int(p_st_body_data, arr_key, u32_id);
    
    return FR_SUCC;
    
}

/*!
 * \brief hessian 获取 remote
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name      上报名
 *              p_st_parse      解析参数
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_get_remote(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_name, hessian_parse *p_st_parse)
{
    char arr_key[ARRAY_LEN];
    
    //todo
    snprintf(arr_key, sizeof(arr_key), "%s.remoteid", p_name);
    frreport_body_l7_add_int(p_st_body_data, arr_key, p_st_parse->i_ref_id);
    p_st_parse->i_ref_id++;
    
    snprintf(arr_key, sizeof(arr_key), "%s.remote.type", p_name);
    frhessian_get_string(p_st_data, p_st_body_data, arr_key);
    
    snprintf(arr_key, sizeof(arr_key), "%s.remote.url", p_name);
    frhessian_read_object(p_st_data, p_st_body_data, arr_key, p_st_parse);
    
    return FR_SUCC;
    
}

/*!
 * \brief hessian this.readObject() 不带参数
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name 上报名
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_read_object(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , char *p_name, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_name == NULL || p_st_parse == NULL)
        return FR_FAIL;
    
    bytebuff_get_char(p_st_data, &u8_tag);
    switch(u8_tag)
    {
        case 'N':
            return frreport_body_l7_add_str(p_st_body_data, p_name, "NULL");
    
        case 'T':
            return frreport_body_l7_add_int(p_st_body_data, p_name, 1);
    
        case 'F':
            return frreport_body_l7_add_int(p_st_body_data, p_name, 0);
    
        case 'I':
            return frhessian_get_int(p_st_data, p_st_body_data, p_name);
    
        case 'L':
            return frhessian_get_long(p_st_data, p_st_body_data, p_name);
    
        case 'D':
            return frhessian_get_double(p_st_data, p_st_body_data, p_name);
    
        case 'd':
            return frhessian_get_date(p_st_data, p_st_body_data, p_name);
    
        case 'x':
        case 'X':
            //todo
            return frhessian_get_string(p_st_data, p_st_body_data, p_name);
    
        case 's':
        case 'S':
            bytebuffer_backspace(p_st_data, 1);
            return frhessian_get_string(p_st_data, p_st_body_data, p_name);
    
        case 'b':
        case 'B':
            bytebuffer_backspace(p_st_data, 1);
            return frhessian_get_bytes(p_st_data, p_st_body_data, p_name);
    
        case 'V':
            frhessian_get_type(p_st_data, p_st_body_data, p_name, p_st_parse);
            return frhessian_get_list(p_st_data, p_st_body_data, p_name, p_st_parse);
    
        case 'M':
            frhessian_get_type(p_st_data, p_st_body_data, p_name, p_st_parse);
            return frhessian_get_map(p_st_data, p_st_body_data, p_name, p_st_parse);
    
        case 'R':
            return frhessian_get_ref(p_st_data, p_st_body_data,p_name, p_st_parse);
        
        case 'r':
            return frhessian_get_remote(p_st_data, p_st_body_data, p_name, p_st_parse);
            
        default:
            FRLOG_ERROR_DETAIL("unknown code for readObject at %d", u8_tag);
            bytebuffer_backspace(p_st_data, 1);
            return FR_FAIL;
    }
    
    return FR_FAIL;
}


/*!
 * \brief hessian this.readObject() 带参数
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              p_name 上报名
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_read_object0(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , char *p_name, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    char *p_type = NULL;
    char arr_key[ARRAY_LEN];
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_name == NULL || p_st_parse == NULL)
        return FR_FAIL;
    
    bytebuff_get_char(p_st_data, &u8_tag);
    
    switch (u8_tag)
    {
        case 'N':
            return frreport_body_l7_add_str(p_st_body_data, p_name, "NULL");
    
        case 'M':
            p_type = frhessian_read_type(p_st_data, p_st_body_data, p_st_parse);
            if(p_type == NULL)
            {
//                url2 = this._serializerFactory.getDeserializer(cl);
//                return url2.readMap(this);
                
                return FR_SUCC;
            }
            else
            {
//                url2 = this._serializerFactory.getObjectDeserializer(value);
//                return url2.readMap(this);
                snprintf(arr_key, sizeof(arr_key), "%s.type", p_name);
                return frreport_body_l7_add_str(p_st_body_data, arr_key, p_type);
            }

        
        case 82:
//            int value1 = this.parseInt();
//            return this._refs.get(value1);
            snprintf(arr_key, sizeof(arr_key), "%s.ref", p_name);
            return frhessian_get_int(p_st_data, p_st_body_data, arr_key);
        
        case 86:
            p_type = frhessian_read_type(p_st_data, p_st_body_data, p_st_parse);
//            value = this.readType();
//            int url1 = this.readLength();
//            Deserializer reader = this._serializerFactory.getObjectDeserializer(value);
//            if(cl != reader.getType() && cl.isAssignableFrom(reader.getType())) {
//                return reader.readList(this, url1);
//            }
//
//            reader = this._serializerFactory.getDeserializer(cl);
//            Object v = reader.readList(this, url1);
//            return v;
            //todo
            frfree(p_type);
            return FR_SUCC;
        
        case 114:
//            value = this.readType();
//            String url = this.readString();
//            return this.resolveRemote(value, url);
            p_type = frhessian_read_type(p_st_data, p_st_body_data, p_st_parse);
            //todo
            if(frhessian_get_string(p_st_data, p_st_body_data, p_type) == FR_FAIL)
            {
                frfree(p_type);
                return FR_FAIL;
            }
            
            frfree(p_type);
            return FR_SUCC;
        
        default:
            return FR_FAIL;
        
    }
    
}


/*!
 * \brief hessian 调用过程解析
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_start_call(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    int i_ret = 0;
    char *p_stub = NULL;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_YES || p_st_parse == NULL)
        return FR_FAIL;
    
    i_ret = frhessian_read_call(p_st_data, p_st_body_data,i_use_request, p_prefix, p_st_parse);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    while ((p_stub = frhessian_read_head(p_st_data, p_st_body_data,i_use_request, p_prefix, p_st_parse)) != NULL)
    {
        i_ret = frhessian_read_object(p_st_data, p_st_body_data, p_stub, p_st_parse);
        if(i_ret == FR_FAIL)
        {
            frfree(p_stub);
            return FR_FAIL;
        }
        
        frfree(p_stub);
    }
    
    i_ret = frhessian_read_method(p_st_data, p_st_body_data,i_use_request, p_prefix, p_st_parse);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    return FR_SUCC;
    
}



/*!
 * \brief hessian 调用的方法 参数
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_complete_call(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_YES || p_st_parse == NULL)
        return FR_FAIL;
    
    // \x7a
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 122)
    {
        bytebuffer_backspace(p_st_data, 1);
        return FR_FAIL;
    }
    
    return FR_SUCC;
    
}

/*!
 * \brief hessian reply
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_read_reply(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    uint8_t u8_major = 0, u8_minor = 0;
    int i_ret = 0;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_NO || p_st_parse == NULL)
        return FR_FAIL;
    
    // 114
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 114)
    {
        FRLOG_ERROR_DETAIL("expected hessian reply");
        bytebuffer_backspace(p_st_data, 1);
        return FR_FAIL;
    }
    
    i_ret = bytebuff_get_char(p_st_data, &u8_major);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    i_ret = bytebuff_get_char(p_st_data, &u8_minor);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    // 114
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag == 102)
    {
        FRLOG_ERROR_DETAIL("prepareFault");
        bytebuffer_backspace(p_st_data, 1);
        return FR_FAIL;
    }
    else
    {
//        this._peek = tag;
//        Object value = this.readObject(expectedClass);
//        this.completeValueReply();
//        return value;
        bytebuffer_backspace(p_st_data, 1);
        return frhessian_read_object(p_st_data, p_st_body_data, p_prefix, p_st_parse);
    }
    
    return FR_SUCC;
    
}


/*!
 * \brief hessian start reply
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_start_reply(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    uint8_t u8_major = 0, u8_minor = 0;
    int i_ret = 0;
    char arr_key[ARRAY_LEN];
    
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 114)
    {
        FRLOG_ERROR_DETAIL("expected hessian reply at %d", u8_tag);
        return FR_FAIL;
    }
    
    i_ret = bytebuff_get_char(p_st_data, &u8_major);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    i_ret = bytebuff_get_char(p_st_data, &u8_minor);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    snprintf(arr_key, sizeof(arr_key), "%s.version", p_prefix);
    
    return FR_SUCC;
}

/*!
 * \brief hessian start reply body
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_start_reply_body(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;

    
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag == 102)
    {
        FRLOG_ERROR_DETAIL("prepareFault");
        return FR_FAIL;
    }
    
    bytebuffer_backspace(p_st_data, 1);
    return FR_SUCC;
    
}



/*!
 * \brief hessian start reply body
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_complete_reply(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data
        , int i_use_request, char *p_prefix, hessian_parse *p_st_parse)
{
    uint8_t u8_tag = 0;
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL
       || i_use_request != FR_NO || p_st_parse == NULL)
        return FR_FAIL;
    
    bytebuff_get_char(p_st_data, &u8_tag);
    if(u8_tag != 122)
    {
        FRLOG_ERROR_DETAIL("expected end of reply");
        return FR_FAIL;
    }

    return FR_SUCC;
    
}


/*!
 * \brief hessian req 解析
 *
 * \arg         p_st_data 数据内容
 *              p_st_body_data 上报数据收集链表
 *              i_use_request 是否为request
 *              p_prefix 上报值前缀名         (hessian  或者 http.hessian)
 *              p_st_parse hessian 数据相关信息
 *
 * \return      FR_SUCC:成功
 *              FR_FAIL:失败
 *
 */
int frhessian_req_read(Frbyte_Packet *p_st_data, Frreport_body_data *p_st_body_data, char *p_prefix)
{
    int i_ret = 0;
    int i_index = 1;
    char arr_key[ARRAY_LEN];
    hessian_parse st_parse = {FR_NO, -1, 0};
    
    if(p_st_data == NULL || p_st_body_data == NULL || p_prefix == NULL)
        return FR_FAIL;
    
    i_ret = frhessian_start_call(p_st_data, p_st_body_data, FR_YES, p_prefix, &st_parse);
    if(i_ret == FR_FAIL)
        return FR_FAIL;
    
    while (frhessian_complete_call(p_st_data, p_st_body_data, FR_YES, p_prefix, &st_parse) == FR_FAIL)
    {
        //todo arg
        snprintf(arr_key, sizeof(arr_key), "%s.arg.%d", p_prefix, i_index);
        i_ret = frhessian_read_object(p_st_data, p_st_body_data, arr_key, &st_parse);
        if(i_ret == FR_FAIL)
            return FR_FAIL;
        
        i_index++;
    }
    
    return frhessian_complete_call(p_st_data, p_st_body_data, FR_YES, p_prefix, &st_parse);
    
}

